//
//  BLEFilterVC.m
//  Holyiot-DFU
//
//  Created by bill on 2021/4/24.
//

#import "BLEFilterVC.h"
#import "BluetoothCenter.h"
@interface BLEFilterVC ()<UITextFieldDelegate>{
    int lastRSSIValue;
}
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UILabel *filterRSSILab;
@property (weak, nonatomic) IBOutlet UISwitch *filter_switch;

@property (weak, nonatomic) IBOutlet UITextField *majorMinTFD;
@property (weak, nonatomic) IBOutlet UITextField *majorMaxTFD;

@property (weak, nonatomic) IBOutlet UITextField *minorMinTFD;
@property (weak, nonatomic) IBOutlet UITextField *minorMaxTFD;
@property (weak, nonatomic) IBOutlet UISwitch *major_switch;
@property (weak, nonatomic) IBOutlet UISwitch *minor_switch;

@property (weak, nonatomic) IBOutlet UILabel *fileNameLab;
@property (weak, nonatomic) IBOutlet UILabel *fileSizeLab;
@property (weak, nonatomic) IBOutlet UILabel *fileDateLab;
@property (weak, nonatomic) IBOutlet UITextField *dfuTFD;
@property (weak, nonatomic) IBOutlet UITextField *dfuCodeTFD;
@property (weak, nonatomic) IBOutlet UITextField *writeCharacTFD;

@property(nonatomic ,strong) DFURouter *router;
@end

@implementation BLEFilterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"Filter";
    
    self.slider.value = 80;
    lastRSSIValue = -self.slider.maximumValue;
    self.filterRSSILab.text = [NSString stringWithFormat:@"-%ddb",(int)self.slider.value];
    self.filter_switch.on = [BluetoothCenter shareInstance].isFilterRSSI;
    self.major_switch.on = [BluetoothCenter shareInstance].isFilterMajor;
    self.minor_switch.on = [BluetoothCenter shareInstance].isFilterMinor;
    //判断上次是否打开过滤开关
   
    self.slider.value = labs([BluetoothCenter shareInstance].rssi_filter);
    NSLog(@" ---->%d",[BluetoothCenter shareInstance].rssi_filter);
    self.filterRSSILab.text = [NSString stringWithFormat:@"-%ddb",(int)self.slider.value];
    self.majorMinTFD.text = [NSString stringWithFormat:@"%d",[BluetoothCenter shareInstance].majorMin_filter];
    self.majorMaxTFD.text = [NSString stringWithFormat:@"%d",[BluetoothCenter shareInstance].majorMax_filter];
    self.minorMinTFD.text = [NSString stringWithFormat:@"%d",[BluetoothCenter shareInstance].majorMin_filter];
    self.minorMaxTFD.text = [NSString stringWithFormat:@"%d",[BluetoothCenter shareInstance].minorMax_filter];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didSelectedFile) name:@"Select_File_No" object:nil];
}
-(void)didSelectedFile
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([BluetoothCenter shareInstance].firmware) {
        self.fileNameLab.hidden = NO;
        self.fileSizeLab.hidden = NO;
        self.fileDateLab.hidden = NO;
        self.fileNameLab.text = [BluetoothCenter shareInstance].firmware.fileName;
        NSLog(@"-->%@",[BluetoothCenter shareInstance].firmware.fileUrl.absoluteString);
        
        NSError *error;
       NSDictionary *dic = [[NSFileManager defaultManager]attributesOfItemAtPath:[BluetoothCenter shareInstance].firmware.fileUrl.path error:&error];
        
        if (!error) {
            NSLog(@"文件大小 :%ld KB",[dic fileSize]/1000.0);
        }else{
            NSLog(@"读取错误:%@",error);
        }
        self.fileSizeLab.text = [NSString stringWithFormat:@"%llu KB",[dic fileSize]/1000];
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"yyyy-MM-dd";
        self.fileDateLab.text = [formatter stringFromDate:[dic fileCreationDate]];
    }
    if ([BluetoothCenter shareInstance].dfu_name.length>0) {
        self.dfuTFD.text = [BluetoothCenter shareInstance].dfu_name;
    }else{
        if (self.dfuTFD.text.length>0) {
            [BluetoothCenter shareInstance].dfu_name = self.dfuTFD.text;
        }
    }
    if ([BluetoothCenter shareInstance].dfu_code.length>0) {
        self.dfuCodeTFD.text = [BluetoothCenter shareInstance].dfu_code;
    }
    if ([BluetoothCenter shareInstance].writeCharacteristics.length>0) {
        self.writeCharacTFD.text = [BluetoothCenter shareInstance].writeCharacteristics;
    }
}
- (IBAction)touchUpInside:(UISlider *)sender {
    if (-sender.value>lastRSSIValue) {
        //[self searchDevice:nil];
    }
    lastRSSIValue = -sender.value;
    
    //保存起来
    if (self.filter_switch.isOn) {
        [BluetoothCenter shareInstance].rssi_filter = -(int)sender.value;
    }
    NSLog(@" ---->%d",[BluetoothCenter shareInstance].rssi_filter);
}

- (IBAction)valueChangeAction:(UISlider*)slider {
    self.filterRSSILab.text = [NSString stringWithFormat:@"-%ddb",(int)slider.value];
    [BluetoothCenter shareInstance].rssi_filter = -(int)slider.value;
    NSLog(@" ---->%d",[BluetoothCenter shareInstance].rssi_filter);
}
//信号过滤
- (IBAction)switch_valueChange:(UISwitch *)sender {
    [BluetoothCenter shareInstance].isFilterRSSI = sender.on;
    if (sender.isOn) {
        [BluetoothCenter shareInstance].rssi_filter = (int)-self.slider.value;
    }
    NSLog(@" ---->%d",[BluetoothCenter shareInstance].rssi_filter);
}

- (IBAction)majorSwitchValueChange:(UISwitch *)sender {
    [BluetoothCenter shareInstance].isFilterMajor = sender.isOn;
    if (sender.isOn) {
        [BluetoothCenter shareInstance].majorMin_filter = [self.majorMinTFD.text intValue];
        [BluetoothCenter shareInstance].majorMin_filter = [self.majorMinTFD.text intValue];
    }
}

- (IBAction)minorSwitchValueChange:(UISwitch *)sender {
    [BluetoothCenter shareInstance].isFilterMinor = sender.isOn;
    if (sender.isOn) {
        [BluetoothCenter shareInstance].minorMin_filter = [self.minorMinTFD.text intValue];
        [BluetoothCenter shareInstance].minorMax_filter = [self.minorMaxTFD.text intValue];
        
    }
    
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"%s",__func__);
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"%s",__func__);
    return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//    NSLog(@"--->输入%@",string);
//    if (![string isEqualToString:@""]) {
//       int value = [[textField.text stringByAppendingString:string]intValue];
//        if (value>65535) {
//            [MBProgressHUD showError:@"up to 65535"];
//            return NO;
//        }else{
//            if (textField==self.majorMinTFD) {
////                if (value> [self.majorMaxTFD.text intValue]) {
////                    [MBProgressHUD showError:@"The minimum value cannot be greater than the maximum value"];
////                    return NO;
////                }
//                [BluetoothCenter shareInstance].majorMin_filter = value;
//            }
//            if (textField==self.majorMaxTFD) {
//                [BluetoothCenter shareInstance].majorMax_filter = value;
//            }
//            if (textField==self.minorMinTFD) {
////                if (value> [self.minorMaxTFD.text intValue]) {
////                    [MBProgressHUD showError:@"The minimum value cannot be greater than the maximum value"];
////                    return NO;
////                }
//                [BluetoothCenter shareInstance].minorMin_filter = value;
//            }
//            if (textField==self.minorMaxTFD) {
//                [BluetoothCenter shareInstance].minorMax_filter = value;
//            }
//            NSLog(@"majorMin:%d\nmajorMax:%d\nminorMin:%d\nminorMax:%d",[BluetoothCenter shareInstance].majorMin_filter,[BluetoothCenter shareInstance].majorMax_filter,[BluetoothCenter shareInstance].minorMin_filter,[BluetoothCenter shareInstance].minorMax_filter);
//        }
//    }
    return YES;
}
//选择文件
- (IBAction)selectFile:(id)sender {
    
    DFURouter *router = [[DFURouter alloc]initWithNavigationController:self.navigationController];
    [router openSelectFileVC];
 
}

- (IBAction)sure:(id)sender {
    [self.view endEditing:YES];
    [BluetoothCenter shareInstance].dfu_name = self.dfuTFD.text;
}
- (IBAction)dfuCodeSure:(id)sender {
    if (self.dfuCodeTFD.text.length<2) {
        [MBProgressHUD showError:@"升级码有误,请重新输入"];
        return;
    }
    if ([self.dfuCodeTFD.text containsString:@"0x"]) {
      NSString *code =  [self.dfuCodeTFD.text stringByReplacingOccurrencesOfString:@"0x" withString:@""];
        [BluetoothCenter shareInstance].dfu_code = code;
    }else{
        [BluetoothCenter shareInstance].dfu_code = self.dfuCodeTFD.text;
    }
    [self.view endEditing:YES];
}

- (IBAction)writeCharacSure:(id)sender {
    NSString *writeCharacReg = @"([0-9a-fA-F]{8}-)([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",writeCharacReg];
    if ([regextestmobile evaluateWithObject:self.writeCharacTFD.text] == NO){
       
        [MBProgressHUD showError:@"UUID格式错误!"];
        
    }else{
        NSLog(@"参数合格..");
        [BluetoothCenter shareInstance].writeCharacteristics = [self.writeCharacTFD.text uppercaseString];
    }
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(paste:)) {
        return YES;
    }
    if (action == @selector(copy:)) {
        return YES;
    }
    if (action == @selector(select:)) {
        return YES;
    }
    if (action == @selector(selectAll:)) {
        return YES;
    }
    if (action == @selector(delete:)) {
        return YES;
    }
    return NO;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
